﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataProcessor.DB.ORM;
using Hjt212Parser;
using log4net;

namespace DataProcessor.Processor
{
    class Util
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Util));
        public static bool IsNewFactor(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                return false;
            }

            return code.Length >= 5;
        }
        private static List<MonitorFactorCode> _contaminants;
        public static MonitorFactorCode GetContaminantByCode(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                return null;
            }

            try
            {
                if (_contaminants == null || MyTimer.IsToTheTime("Util.GetContaminantByCode", 10))
                {
                    using (var ctx = new PlatformEntities())
                    {
                        _contaminants = ctx.MonitorFactorCodes.AsNoTracking().ToList();
                    }
                }

                if (IsNewFactor(code))
                {
                    return _contaminants.FirstOrDefault(t => t.污染物代码.Equals(code));
                }
                else
                {
                    return _contaminants.FirstOrDefault(t => t.污染物代码旧.Equals(code));
                }
              
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;

        }
        private static List<EquipmentInfor> _enterprisePortBuffer;
        /// <summary>
        /// 根据Mn获取排口信息
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        public static EquipmentInfor GetInfoFromMn(string mn)
        {
            try
            {
                if (_enterprisePortBuffer == null || MyTimer.IsToTheTime("Util.GetInfoFromMn", 10))
                {
                    using (var ctx = new PlatformEntities())
                    {
                        _enterprisePortBuffer = ctx.EquipmentInfors.AsNoTracking().ToList();
                    }
                }
                return _enterprisePortBuffer.FirstOrDefault(t => t.企业端数采MN.Equals(mn));

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;
        }
        /// <summary>
        /// 查找串中主污染物代码（根据配置文件中主污染物代码）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetMasterPollutionCode(List<string> source)
        {
            string masterString = DataProcessor.Properties.Settings.Default.MasterPollutants;
            var master = masterString.Split(';');
            return master.FirstOrDefault(item => source.Contains(item));
        }
        public static DateTime OffsetMinute(DateTime time, int minute)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0).AddMinutes(minute);
        }
        public static DateTime TrimToHour(DateTime time, int hours)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0).AddHours(hours);
        }
        public static DateTime TrimToDay(DateTime time, int days)
        {
            return new DateTime(time.Year, time.Month, time.Day).AddDays(days);
        }
        public static DateTime TrimToMonth(DateTime time, int months)
        {
            return new DateTime(time.Year, time.Month, 1).AddMonths(months);
        }
        public static DateTime TrimToYear(DateTime time, int year)
        {
            return new DateTime(time.Year, 1, 1).AddYears(year);
        }
      
    }
}
